import 'package:flutter/services.dart';

extension StringExt on String? {
  bool get isNullOrEmpty => this == null || this!.isEmpty;

  bool equals(String? other, {bool ignoreCase = true}) {
    if (this == null) {
      return other == null;
    }
    if (!ignoreCase) {
      return this == other;
    } else {
      return this?.toLowerCase() == other?.toLowerCase();
    }
  }

  ///复制文本到剪贴板
  void copy() {
    final text = this;
    if (text != null) {
      Clipboard.setData(ClipboardData(text: text));
    }
  }

  ///是否是整形数字
  bool get isInt {
    if (isNullOrEmpty) return false;
    return int.tryParse(this!) != null;
  }

  ///是否是浮点数字
  bool get isDouble {
    if (isNullOrEmpty) return false;
    return double.tryParse(this!) != null;
  }

  ///是否是数字
  bool get isNumber {
    if (isNullOrEmpty) return false;
    return isInt || isDouble;
  }

  ///字符串安全的转int
  int? toIntSafe() {
    if (isNullOrEmpty) return null;
    return int.tryParse(this!);
  }

  ///字符串安全的转double
  double? toDoubleSafe() {
    if (isNullOrEmpty) return null;
    return double.tryParse(this!);
  }
}
